package com.jp.vn.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.vn.Object.Alphabet;

public class PhatAmActivity extends Activity {

	TextView tfTitle, tfHuongDan;
	TextView tfViDu1, tfViDu2, tfViDu3, tfViDu4, tfViDu5, tfViDu6;
	ImageView btnAudio1, btnAudio2, btnAudio3, btnAudio4, btnAudio5, btnAudio6;
	public MediaPlayer mediaPlayer;
	Button btnTruongAm, btnXucAm, btnGhepAmN, btnGhepAmI;
	String vd1_text, vd2_text, vd3_text, vd4_text, vd5_text, vd6_text;
	ArrayList<Alphabet> arrayTruongAm = new ArrayList<Alphabet>();
	ArrayList<Alphabet> arrayXucAm = new ArrayList<Alphabet>();
	ArrayList<Alphabet> arrayGhepAmI = new ArrayList<Alphabet>();
	ArrayList<Alphabet> arrayGhepAmN = new ArrayList<Alphabet>();
	public static int indexType = 1;
	LinearLayout layout_vd;
	ImageView btnMenu;
	int height = 0;
	int width = 0;
	int space = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_phat_am);

		tfTitle = (TextView) findViewById(R.id.tfTitle);
		tfHuongDan = (TextView) findViewById(R.id.tfHuongDan);
		tfViDu1 = (TextView) findViewById(R.id.tfVidu1);
		tfViDu2 = (TextView) findViewById(R.id.tfVidu2);
		tfViDu3 = (TextView) findViewById(R.id.tfVidu3);
		tfViDu4 = (TextView) findViewById(R.id.tfVidu4);
		tfViDu5 = (TextView) findViewById(R.id.tfVidu5);
		tfViDu6 = (TextView) findViewById(R.id.tfVidu6);
		btnAudio1 = (ImageView) findViewById(R.id.btnAudio1);
		btnAudio2 = (ImageView) findViewById(R.id.btnAudio2);
		btnAudio3 = (ImageView) findViewById(R.id.btnAudio3);
		btnAudio4 = (ImageView) findViewById(R.id.btnAudio4);
		btnAudio5 = (ImageView) findViewById(R.id.btnAudio5);
		btnAudio6 = (ImageView) findViewById(R.id.btnAudio6);
		btnTruongAm = (Button) findViewById(R.id.btnTruongAm);
		btnXucAm = (Button) findViewById(R.id.btnXucAm);
		btnGhepAmN = (Button) findViewById(R.id.btnGhepAmn);
		btnGhepAmI = (Button) findViewById(R.id.btnGhepAmi);
		btnMenu = (ImageView) findViewById(R.id.btnMenu);
		layout_vd = (LinearLayout) findViewById(R.id.layout_vidu);
		
		height = (int) getResources().getDimension(R.dimen.menupopup_height);
		width = (int) getResources().getDimension(R.dimen.menupopup_width);
		space = (int) getResources().getDimension(R.dimen.menu_height);

		readData(1);
		dumyDataTruongAm();
		setData(arrayTruongAm);

		Typeface type = Typeface.createFromAsset(getAssets(),"fonts/tekton.ttf");
		tfTitle.setTypeface(type, Typeface.BOLD_ITALIC);
		tfHuongDan.setTypeface(type, Typeface.BOLD_ITALIC);
		tfViDu1.setTypeface(type, Typeface.ITALIC);
		tfViDu2.setTypeface(type, Typeface.ITALIC);
		tfViDu3.setTypeface(type, Typeface.ITALIC);
		tfViDu4.setTypeface(type, Typeface.ITALIC);
		tfViDu5.setTypeface(type, Typeface.ITALIC);

		btnMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				PopupMenu menu = new PopupMenu(v, height, width);
				menu.showPopup(space + 5);
			}
		});

		btnTruongAm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				arrayTruongAm.clear();
				tfHuongDan.setText("");
				indexType = 1;
				readData(indexType);
				dumyDataTruongAm();
				setData(arrayTruongAm);
			}
		});

		btnXucAm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				arrayXucAm.clear();
				tfHuongDan.setText("");
				indexType = 2;
				readData(indexType);
				dumyDataXucAm();
				setData(arrayXucAm);
			}
		});

		btnGhepAmI.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tfHuongDan.setText("");
				indexType = 3;
			}
		});

		btnGhepAmN.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				arrayGhepAmN.clear();
				tfHuongDan.setText("");
				indexType = 4;
				readData(indexType);
				dumyDataGhepAmN();
				setData(arrayGhepAmN);
			}
		});

		btnAudio1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					playSound(vd1_text, indexType);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		btnAudio2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					playSound(vd2_text, indexType);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		btnAudio3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					playSound(vd3_text, indexType);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		btnAudio4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					playSound(vd4_text, indexType);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		btnAudio5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					playSound(vd5_text, indexType);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		btnAudio6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					playSound(vd6_text, indexType);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

	}

	public void readData(int type) {
		String data;
		InputStream in = null;
		String kq = "";

		if (type == 1) {
			in = getResources().openRawResource(R.drawable.hdtruongam);
		}

		if (type == 2) {
			in = getResources().openRawResource(R.drawable.hdxucam);
		}

		if (type == 3) {

		}

		if (type == 4) {
			in = getResources().openRawResource(R.drawable.hdghepamn);
		}

		InputStreamReader inreader = new InputStreamReader(in);
		BufferedReader bufreader = new BufferedReader(inreader);
		if (in != null) {
			try {
				while ((data = bufreader.readLine()) != null) {
					kq += data.toString() + "\n";
				}
				in.close();
				tfHuongDan.setText(kq);
			} catch (IOException ex) {
				Log.e("ERROR", ex.getMessage());
			}
		}
	}

	public void dumyDataTruongAm() {
		arrayTruongAm.add(new Alphabet("おばあさん", "obaasan", 0));
		arrayTruongAm.add(new Alphabet("おじいさん", "ojiisan", 0));
		arrayTruongAm.add(new Alphabet("おねえさん", "oneesan", 0));
		arrayTruongAm.add(new Alphabet("おおきい", "ookii", 0));
		arrayTruongAm.add(new Alphabet("パーティー", "paty", 0));
		arrayTruongAm.add(new Alphabet("アー", "aa", 0));
	}

	public void dumyDataXucAm() {
		arrayXucAm.add(new Alphabet("がっこう", "gakkou", 0));
		arrayXucAm.add(new Alphabet("きって", "kitte", 0));
		arrayXucAm.add(new Alphabet("こっち", "kocchi", 0));
		arrayXucAm.add(new Alphabet("", "", 0));
		arrayXucAm.add(new Alphabet("ざっし", "zasshi", 0));
		arrayXucAm.add(new Alphabet("きっぷ", "kippu", 0));
	}

	public void dumyDataGhepAmN() {
		arrayGhepAmN.add(new Alphabet("あん", "an", 0));
		arrayGhepAmN.add(new Alphabet("てん", "ten", 0));
		arrayGhepAmN.add(new Alphabet("ぱん", "pan", 0));
		arrayGhepAmN.add(new Alphabet("ノン", "non", 0));
		arrayGhepAmN.add(new Alphabet("オン", "on", 0));
		arrayGhepAmN.add(new Alphabet("トン", "ton", 0));
	}

	public void playSound(String fileName, int type) throws IOException {
		AssetFileDescriptor afd = null;

		if (type == 1) {
			afd = this.getAssets()
					.openFd("TruongAm_voice/" + fileName + ".ogg");
		}

		if (type == 2) {
			afd = this.getAssets().openFd("XucAm_voice/" + fileName + ".ogg");
		}

		if (type == 3) {
			afd = this.getAssets().openFd("GhepAmI_voice/" + fileName + ".ogg");
		}

		if (type == 4) {
			afd = this.getAssets().openFd("GhepAmN_voice/" + fileName + ".ogg");
		}

		mediaPlayer = new MediaPlayer();
		mediaPlayer.setDataSource(afd.getFileDescriptor(),
				afd.getStartOffset(), afd.getLength());
		mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mp.start();
			}
		});

		mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

					@Override
					public void onCompletion(MediaPlayer mp) {
						mp.release();
					}
				});

		mediaPlayer.prepareAsync();
	}

	public void setData(ArrayList<Alphabet> arrayPhatAm) {

		tfViDu1.setText(arrayPhatAm.get(0).getLetter());
		tfViDu2.setText(arrayPhatAm.get(1).getLetter());
		tfViDu3.setText(arrayPhatAm.get(2).getLetter());
		tfViDu4.setText(arrayPhatAm.get(3).getLetter());
		tfViDu5.setText(arrayPhatAm.get(4).getLetter());
		tfViDu6.setText(arrayPhatAm.get(5).getLetter());

		vd1_text = arrayPhatAm.get(0).getMeaning();
		vd2_text = arrayPhatAm.get(1).getMeaning();
		vd3_text = arrayPhatAm.get(2).getMeaning();
		vd4_text = arrayPhatAm.get(3).getMeaning();
		vd5_text = arrayPhatAm.get(4).getMeaning();
		vd6_text = arrayPhatAm.get(5).getMeaning();
	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
		finish();
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		width = layout_vd.getWidth();
		height = layout_vd.getHeight();
		space = (int) getResources().getDimension(R.dimen.menu_height);
	}

}
